import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class 消费模型 {

    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        //构建两个队列，队列的意思是，当前执行的线程不满足任务需要取等待
        Condition notEmpty = lock.newCondition();
        Condition notFull = lock.newCondition();
        Queue<Object> queue = new ArrayBlockingQueue<Object>(10);
        int queueSize = 10;
        //消费者线程
        Thread cThread = new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                try {
                    //如果队列里面没有了则等待要不然就去消费
                    while (true) {
                        while (0 == queue.size()) {
                            System.out.println("队列已经为空了不能消费了需要等待");
                            notEmpty.await();
                        }
                        Object poll = queue.poll();
                        notFull.signalAll();
                        System.out.println(poll);
                    }
                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }

            }
        });
        //生产者模型
        Thread pThread = new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                try {
                    while (true) {
                        //如果队列已经满了则需要等待
                        while (queueSize == queue.size()) {
                            System.out.println("队列已经为满了不能生产了需要等待");
                            notFull.await();
                        }
                        queue.add(1);
                        System.out.println(1);
                        notEmpty.signalAll();
                    }
                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }

            }
        });
        cThread.start();
        pThread.start();
    }
}




class Test222{
    public static void main(String[] args) {
        int a=0;
        if(a==1 && a==2){
            System.out.println("222");
        }
    }
}